/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.model;

import offset.nodes.client.model.XmlCharacterFilter;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

/**
 * Facilitate writing to a ContentHandler
 *
 * @author Walter Lütgenau
 */
public class SaxUtil {
    ContentHandler handler;

    public SaxUtil(ContentHandler handler) {
        this.handler = handler;
    }
    
    public void endElement(String name) throws SAXException {
        handler.endElement("", "", name);
    }

    public void startElement(String name, Attributes atts) throws SAXException {
        handler.startElement("", "", name, atts);
    }

    public void startElement(String name) throws SAXException {
        startElement(name, null);
    }

    public void addAttribute(AttributesImpl atts, String name, String value) throws SAXException {
        atts.addAttribute("", "", name, "CDATA", XmlCharacterFilter.filterAttributeValue(value));
    }

    public void characters(String value) throws SAXException {
        handler.characters(value.toCharArray(), 0, value.length());
    }


}
